import json
from config.logger import setup_logging
import shutil
import asyncio
import os
import random
import time
import math
from core.handle.receiveAudioHandle import startToChat

logger = setup_logging()

async def handleUnityMessage(conn, message):
    try:
        msg_json = json.loads(message)
        pos = json.loads(msg_json['text'])

        org = [pos['x'], pos['y'], pos['z']]
        target = [0,0,-2]
        target2 = [-1.5,0,2]
        target3 = [1.5,0,2]

        dist = math.sqrt((target[0] - org[0])**2 + (0)**2 + (target[2] - org[2])**2)
        dist2 = math.sqrt((target2[0] - org[0])**2 + (0)**2 + (target2[2] - org[2])**2)
        dist3 = math.sqrt((target3[0] - org[0])**2 + (0)**2 + (target3[2] - org[2])**2)

        distTemp = 1.8
        conn.server.hasTarget = True
        conn.client_abort = False

        print(dist)
        print(dist2)
        print(dist3)
        
        
        if (dist < distTemp):
            print(dist)
            conn.server.hasTarget = True
            
            if (conn.server.role!='role1'):
                # 回复位置信息
                conn.server.role='role1'
                await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist, "role":"Role1"}))
                await startToChat(conn, "切换角色霸王龙,姓名阿八")
            
            
            # conn.server.test(conn,json.dumps({"type":"chat", "text":"切换角色三角龙,姓名阿三"}))
            # conn.server.esp32_message(json.dumps({"type":"chat", "text":"切换角色三角龙,姓名阿三"}))
        elif (dist2< distTemp):
            print(dist2)
            conn.server.hasTarget = True
            
            if (conn.server.role!='role2'):
                # 回复位置信息
                conn.server.role='role2'
                await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist, "role":"Role2"}))
                await startToChat(conn, "切换角色三角龙,姓名阿三")
            # 回复位置信息
            # await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist2, "role":"Role2"}))
            # await startToChat(conn, "切换角色霸王龙,姓名阿八")
            # conn.server.test(conn,json.dumps({"type":"chat", "text":"切换角色霸王龙,姓名阿八"}))
            # conn.server.esp32_message(json.dumps({"type":"chat", "text":"切换角色霸王龙,姓名阿八"}))
        elif (dist3< distTemp):
            print(dist3)
            conn.server.hasTarget = True
            
            if (conn.server.role!='role3'):
                # 回复位置信息
                conn.server.role='role3'
                await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist, "role":"Role3"}))
                await startToChat(conn, "切换角色翼龙,姓名阿二")
            
            # # 回复位置信息
            # await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist3, "role":"Role3"}))
            # await startToChat(conn, "切换角色翼龙,姓名阿二")
            # conn.server.test(conn,json.dumps({"type":"chat", "text":"切换角色翼龙,姓名阿二"}))
            # conn.server.esp32_message(json.dumps({"type":"chat", "text":"切换角色梁龙,姓名阿二"}))
        else:
            print("none target")
            # 回复位置信息
            await conn.websocket.send(json.dumps({"type":"changeRole", "dist": dist3, "role":""}))
            
            if conn.server.role!='': 
                # 设置成打断状态，会自动打断llm、tts任务
                conn.client_abort = True
                # 打断客户端说话状态
                await conn.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": conn.session_id}))
                conn.clearSpeakStatus()   
            
            conn.server.role=''

        print(conn.server.role)
    except json.JSONDecodeError:
        print("JSONDecodeError")

    # await conn.websocket.send(json.dumps({"res":"222"}))